package com.knife.bbs.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.knife.bbs.business.ActiveUser;
import com.knife.bbs.business.BBSDirService;
import com.knife.bbs.business.BBSRights;
import com.knife.bbs.business.VoteService;
import com.knife.bbs.domain.Vote;
import com.knife.bbs.domain.VoteItem;
import com.knife.util.CommUtil;
import com.knife.web.ActionContext;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;
import com.knife.web.tools.IPageList;

public class VoteAction extends BBSAction {
	private VoteService voteService = new VoteService();

	private BBSDirService dirService = BBSDirService.getInstance();

	public Page doInit(WebForm form, Module module) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page doList(WebForm form, Module module) {
		if (!BBSRights.checkRights(new Vote(), "list", getCurrentUser()))
			return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
		return doQuery(form, module);
	}

	private ActiveUser getCurrentUser() {
		ActiveUser user = (ActiveUser) ActionContext.getContext().getSession()
				.getAttribute("bbsuser");
		return user;
	}

	public Page doVoting(WebForm form, Module module) {
		String voteitemid = CommUtil.null2String(form.get("chioc"));
		VoteItem vi = voteService.getVoteItem(voteitemid);

		if (vi != null) {
			int num = vi.getVotenum().intValue();
			vi.setVotenum(new Integer(num + 1));
			voteService.update(vi);
			form.addResult("msg", "投票成功");
		}
		String cid = voteService.getVote(vi.getVoteid()).getDocid();
		return new Page("result",
				contextPath+"/bbsdoc.do?parameter=show&&cid=" + cid, "url");
	}

	public Page doResult(WebForm form, Module module) {
		String cid = CommUtil.null2String(form.get("cid"));
		int max = 0;
		if (cid != null) {
			Vote vote = voteService.getVote(cid);
			List vi = voteService.getVoteItems(vote);

			for (Iterator it = vi.iterator(); it.hasNext();) {
				max += ((VoteItem) it.next()).getVotenum().intValue();
			}

			form.addResult("vote", vote);
			form.addResult("voteitem", vi);
			form.addResult("max", new Integer(max));
		}
		return module.findPage("/"+skin+"/","result");
	}

	public Page doQuery(WebForm form, Module module) {
		int currentPage = CommUtil.null2Int(form.get("page"));
		int pageSize = CommUtil.null2Int(form.get("pageSize"));
		if (currentPage < 1)
			currentPage = 1;
		if (pageSize < 1)
			pageSize = 15;

		IPageList pList = voteService.queryVotes("1=1", pageSize, currentPage);
		if (pList != null) {
			com.knife.util.CommUtil.saveIPageList2WebForm(pList, form);
		}
		return module.findPage("/"+skin+"/","list");
	}

	public Page doDel(WebForm form, Module module) {
		String voteid = CommUtil.null2String(form.get("cid"));
		if (voteService.delVote(voteid)) {
			form.addResult("msg", "删除成功");
			return doQuery(form, module);
		}
		return null;
	}

	public Page doCreate(WebForm form, Module module) {
		List dirs = dirService.getRootDirs();
		form.addResult("dirs", dirs);
		return module.findPage("/"+skin+"/","init");
	}

	public Page doAddItem(WebForm form, Module module) {
		String dirid = CommUtil.null2String(form.get("dirid"));
		int itemnum = CommUtil.null2Int(form.get("itemnum"));
		System.out.println(itemnum);
		List items = new ArrayList(itemnum);
		for (int i = 0; i < itemnum; i++) {
			items.add(i, new Integer(i));
		}
		form.addResult("itemnum", new Integer(itemnum));
		form.addResult("nums", items);
		form.addResult("dirid", dirid);
		return module.findPage("/"+skin+"/","add");
	}

	public Page doAdd(WebForm form, Module module) {
		int itemnum = CommUtil.null2Int(form.get("itemnum"));
		String dirid = CommUtil.null2String(form.get("dirid"));
		String[] items = new String[itemnum];
		for (int i = 0; i < itemnum; i++) {
			items[i] = CommUtil.null2String(form.get("item" + i));
		}
		String votetitle = CommUtil.null2String(form.get("title"));
		String voteinfo = CommUtil.null2String(form.get("info"));
		Vote vote = new Vote();
		vote.setDate(new Date());
		vote.setInfo(voteinfo);
		vote.setTitle(votetitle);
		vote.setNum(new Integer(0));
		voteService.add(dirid, vote, items);
		form.addResult("msg", "添加成功");
		return this.doQuery(form, module);
	}

}
